;/******************************************************************************
;//             INTEL CORPORATION PROPRIETARY INFORMATION
;//  This software is supplied under the terms of a license agreement or
;//  nondisclosure agreement with Intel Corporation and may not be copied
;//  or disclosed except in accordance with the terms of that agreement.
;//    Copyright(c) 2000 - 2001 Intel Corporation. All Rights Reserved.
;//
;//  VSS: 
;//     $Workfile: ijdhufa.s $
;//     $Revision: 1 $
;//     $Date: 01-03-05 16:06 $
;//     $Archive: /ipp_sw/ipp20sil/src/arm_ads/image/jpeg/ijdhufa.s $
;//
;//  Description:
;//      Intel(R) Integrated Performance Primitives - JPEG Helper file.
;//      This file contains Huffman decoding (Baseline) functions.
;//
;//  History:
;//     Date         Author        Changes
;//     2000/6/20   Jerry Hu       Created (Version 0.1 Baseline)
;//     2001/2/10   Jerry Hu       Modified (Version 0.2 Baseline)
;//
;********************************************************************************/

;******************************************************************************
; Marco data
;******************************************************************************
_OFFSET_MAX_MINCODE  EQU 36
_OFFSET_MAXCODE      EQU 36
_OFFSET_MAX_VALPTR   EQU 36
_OFFSET_VALPTR       EQU 72
_OFFSET_VPTR_LOOK    EQU 36
_OFFSET_LOOKBITSVAL  EQU 108
_OFFSET_LOOK_VAL     EQU 1024
_OFFSET_HUFFVAL      EQU 1132

_HUFF_PREVIEWEDBITS EQU 16  
_HUFF_LOOKEDBITS    EQU 8 
_HUFF_LOOKEDBITS_1  EQU 9 
_HUFF_ZEROLEN       EQU 1128

;******************************************************************************
; FUNCTION:   _FillPreviewBits
;  r0->code
;  r4->pSrc(StreamBuffer)
;  r8->UsedLen
;  r9->Marker(Count)
;  r10->PreviewBits
;  r11->nNumValidBits
;  r14->lr
;  return r11
;  _FillPreviewBits r0, r4, r8, r9, r10, r11
;******************************************************************************
    MACRO
   _FillPreviewBits $code, $stream, $usedlen, $marker, $previewbits, $numvalidbits
    CMP    $marker,   #0              ; if (0<marker) {
    BGT    %F9                        ;    return nNumValidBits;
                                      ; }   

    CMP    $numvalidbits,  #_HUFF_PREVIEWEDBITS 
                                      ; if (nNumValidBits>=_HUFF_PREVIEWEDBITS)   {
    BGE    %F9                        ;    return nNumValidBits;
                                      ; }   
    
3
    LDRB   $code,    [$stream],  #1   ; code=*pStreamBuffer++
    ADD    $usedlen, $usedlen,   #1   ; nUsedBytesLen++

    CMP    $code,   #0xFF
    BNE    %F4
    
1
    LDRB   $code, [$stream],    #1    ; code=*pStreamBuffer++
    ADD    $usedlen, $usedlen,  #1    ; nUsedBytesLen++
    CMP    $code,    #0xFF
    BEQ    %B1

    CMP    $code,   #0
    MOVEQ  $code,   #0xFF             ; code=0xFF
    MOVNE  $marker,     $code         ; marker=code 
    BNE    %F9                        ; return

4
    ADD    $numvalidbits, $numvalidbits,   #8
                                       ; nNumValidBits+=8;
    ORR    $previewbits,  $code,  $previewbits,   LSL #8 
                                       ; PreviewBits=code|(PreviewBits<<8)

    CMP    $numvalidbits,  #_HUFF_PREVIEWEDBITS 
                                       ; while (nNumValidBits<_HUFF_PREVIEWEDBITS)
    BLT    %B3
9
    MEND

    END
